const util = require('../../utils/util.js')
const app = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        member_info: {},
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        //请求分享数据
        wx.request({
            url: app.globalData.ApiUrl + '/index.php?act=wx_share&op=get_share',
            data: {},
            method: 'GET',
            dataType: 'json',
            success: res => {
                if (res.data.code == '200') {
                    this.setData({
                        share_default_title: res.data.datas.title,
                        share_default_image_url: res.data.datas.imageUrl
                    })
                }
            }
        })
        util.share();
    },
    login: function () {
        wx.getSetting({
            success: res => {
                if (res.authSetting['scope.userInfo']) {
                    wx.navigateTo({
                        url: '../chooselogin/chooselogin'
                    })
                } else {
                    wx.navigateTo({
                        url: '../login/login'
                    })
                }
            }
        })
    },
    memberSitting: function () {
        wx.navigateTo({
            url: '../member_account/member_account'
        })
    },
    onReady: function () {

    },
    onShow: function () {
        util.tabCalCartNum();
        var self = this
        wx.getStorage({
            key: 'key',
            success: res => {
                self.setData({
                    key: res.data
                })
                wx.request({
                    url: app.globalData.ApiUrl + "/index.php?act=member_index",
                    data: {
                        key: res.data
                    },
                    header: {
                        'content-type': 'application/x-www-form-urlencoded'
                    },
                    method: 'POST',
                    success: (result) => {
                        if (result.data.code == 200) {
                            self.setData({
                                member_info: result.data.datas.member_info
                            })
                        } else {
                            wx.removeStorage({
                                key: 'username',
                                success(res) {
                                }
                            })
                            wx.removeStorage({
                                key: 'member_mobile',
                                success(res) {
                                }
                            })

                            wx.removeStorage({
                                key: 'key',
                                success(res) {
                                    self.setData({
                                        key: ''
                                    })
                                    wx.redirectTo({
                                        url: '../member/member'
                                    })
                                }
                            })
                        }

                    }
                });
                //红包start
                var timestamp = Date.parse(new Date()) / 1000;
                wx.getStorage({
                    key: "packageTime",
                    success: function (rest) {
                        if ((timestamp - rest.data) > 43200) {
                            self.giftBag = self.selectComponent("#gift_bag");
                            self.giftBag.getPackage(res.data);
                        }
                    },
                    fail: function () {
                        self.giftBag = self.selectComponent("#gift_bag");
                        self.giftBag.getPackage(res.data);
                    }
                });
            },
            fail: function () {
                wx.removeStorage({
                    key: 'cart_count',
                    success: function () {
                        wx.removeTabBarBadge({
                            index: 3
                        })
                    }
                })

                self.setData({
                    key: '',
                    member_info: {}
                })
            }
        })
    },
    shareInfo: function () {
        wx.navigateTo({
            url: '../member_share/member_share'
        })
    },
    toMyorder: function () {
        wx.navigateTo({
            url: '../myOrder/myOrder'
        })
    },
    redirectUri: function (e) {
        var self = this
        let uri = e.currentTarget.dataset.uri;
        if (self.data.key) {
            wx.navigateTo({
                url: uri
            })
        } else {
            if (app.globalData.authorized) {
                wx.navigateTo({
                    url: "../chooselogin/chooselogin"
                })
            } else {
                wx.navigateTo({
                    url: "../login/login"
                })
            }
        }
    },
    modifyIcon: function () {
        let self = this;
        wx.chooseImage({
            count: 1,
            sizeType: ['original', 'compressed'], //可选择原图或压缩后的图片
            sourceType: ['album', 'camera'], //可选择性开放访问相册、相机
            success: res => {
                //这里是上传操作
                wx.uploadFile({
                    url: app.globalData.ApiUrl + '/index.php?act=avatar&op=wechat_update_avatar', //里面填写你的上传图片服务器API接口的路径 
                    filePath: res.tempFilePaths[0], //要上传文件资源的路径 String类型 
                    name: 'file', //按个人情况修改，文件对应的 key,开发者在服务器端通过这个 key 可以获取到文件二进制内容，(后台接口规定的关于图片的请求参数)
                    header: {
                        "Content-Type": "multipart/form-data" //记得设置
                    },
                    formData: {
                        //和服务器约定的token, 一般也可以放在header中
                        key: self.data.key
                    },
                    success: function (sdata) {
                        var data = JSON.parse(sdata.data);
                        if (data.code == 200) {
                            wx.showToast({
                                title: data.datas,
                                icon: 'none'
                            })
                        } else {
                            wx.showToast({
                                title: '图片上传失败！',
                                icon: 'none'
                            })
                        }
                        wx.request({
                            url: app.globalData.ApiUrl + "/index.php?act=member_index",
                            data: {
                                key: self.data.key
                            },
                            header: {
                                'content-type': 'application/x-www-form-urlencoded'
                            },
                            method: 'POST',
                            success: (result) => {
                                self.setData({
                                    member_info: result.data.datas.member_info
                                })
                            }
                        });
                    }
                })

            }
        })
    },
    onShareAppMessage: function () {
        let share_key = this.data.key ? this.data.key : '';
        let share_path = '';
        if (util.getCurrentPageUrlWithArgs().indexOf("?") != -1) {
            share_path = util.getCurrentPageUrlWithArgs() + '&share_key=' + share_key;
        } else {
            share_path = util.getCurrentPageUrlWithArgs() + '?share_key=' + share_key;
        }
        return {
            title: this.data.share_default_title,
            imageUrl: this.data.share_default_image_url,
            path: share_path
        }
    }
})